package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import io.swagger.annotations.ApiOperation;
import org.linlinjava.litemall.db.dao.LitemallSearchHistoryMapper;
import org.linlinjava.litemall.db.dao.SearchHistoryMapper;
import org.linlinjava.litemall.db.domain.LitemallSearchHistory;
import org.linlinjava.litemall.db.domain.LitemallSearchHistoryExample;
import org.linlinjava.litemall.db.vo.SearchHistoryVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class LitemallSearchHistoryService {
    @Resource
    private SearchHistoryMapper searchHistoryMapper;
    @Resource
    LitemallSearchHistoryMapper litemallSearchHistoryMapper;

    public void save(LitemallSearchHistory searchHistory) {
        searchHistory.setAddTime(LocalDateTime.now());
        searchHistory.setUpdateTime(LocalDateTime.now());
        litemallSearchHistoryMapper.insertSelective(searchHistory);
    }

    @ApiOperation(value = "根据用户id查询其搜索记录统计数据的前num条")
    public List<SearchHistoryVo> queryByUidWithOrder(int uid, int num) {
        return searchHistoryMapper.queryByUidWithOrder(uid, num);
    }

    public List<LitemallSearchHistory> queryByUid(int uid) {
        LitemallSearchHistoryExample example = new LitemallSearchHistoryExample();
        example.or().andUserIdEqualTo(uid).andDeletedEqualTo(false);
        example.setDistinct(true);
        return litemallSearchHistoryMapper.selectByExampleSelective(example, LitemallSearchHistory.Column.keyword);
    }

    public List<LitemallSearchHistory> queryByUidAndKeyWord(int uid, String keyword) {
        LitemallSearchHistoryExample example = new LitemallSearchHistoryExample();
        example.or().andUserIdEqualTo(uid).andKeywordEqualTo(keyword).andDeletedEqualTo(false);
        example.setDistinct(true);
        return litemallSearchHistoryMapper.selectByExampleSelective(example, LitemallSearchHistory.Column.keyword);
    }

    public List<LitemallSearchHistory> queryByUserIdAndLimit(Integer userId) {
        return searchHistoryMapper.queryByUserIdAndLimit(userId, 5);
    }

    public void deleteByUid(int uid) {
        LitemallSearchHistoryExample example = new LitemallSearchHistoryExample();
        example.or().andUserIdEqualTo(uid);
        litemallSearchHistoryMapper.logicalDeleteByExample(example);
    }

    public List<LitemallSearchHistory> querySelective(String userId, String keyword, Integer page, Integer size, String sort, String order) {
        LitemallSearchHistoryExample example = new LitemallSearchHistoryExample();
        LitemallSearchHistoryExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(userId)) {
            criteria.andUserIdEqualTo(Integer.valueOf(userId));
        }
        if (!StringUtils.isEmpty(keyword)) {
            criteria.andKeywordLike("%" + keyword + "%");
        }
        criteria.andDeletedEqualTo(false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, size);
        return litemallSearchHistoryMapper.selectByExample(example);
    }
}
